//
//  ViewController.m
//  UI 22 - 数据库
//
//  Created by hesiyao on 16/1/6.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "ViewController.h"
#import "DataBaseManager.h"
#import "Movie.h"
@interface ViewController ()

@end

@implementation ViewController

#pragma mark - 打开数据库
- (IBAction)openDB:(id)sender {
    // 创建单例对象
    DataBaseManager *dbManager = [DataBaseManager shareInstance];
    // 打开数据库
    [dbManager openDB];
    // 常见问题
    // 1 语句错误
    // 5 删除掉数据库重新打开
    // 21 没有打开数据库
}

#pragma mark - 关闭数据库
- (IBAction)closeDB:(id)sender {
    [[DataBaseManager shareInstance] closeDB];
}

#pragma mark - 创建表
- (IBAction)CreateTable:(id)sender {
    [[DataBaseManager shareInstance] createTable];
}

#pragma mark - 删除表
- (IBAction)dropTable:(id)sender {
    [[DataBaseManager shareInstance] dropTable];
}

#pragma mark - 添加
- (IBAction)insert:(id)sender {
    Movie *m = [[Movie alloc] init];
    m.movieName = @"唐人街探案";
    m.pubdate = @"2016-1-6";
    //  插入
    [[DataBaseManager shareInstance] insertMovie:m];
}

#pragma mark - 删除
- (IBAction)delete:(id)sender {
    [[DataBaseManager shareInstance] deleteMovieWithId:2];
}

#pragma mark - 修改
- (IBAction)updete:(id)sender {
    Movie *newM = [[Movie alloc] init];
    newM.pubdate = @"随便";
    newM.movieName = @"星球大战";
    [[DataBaseManager shareInstance] updateMovieWith:newM withId:1];
}

#pragma mark - 查询
- (IBAction)select:(id)sender {
    NSMutableArray *arr = [[DataBaseManager shareInstance] select];
    for (Movie *m in arr) {
        NSLog(@"%@ %@ %ld", m.movieName, m.pubdate, m.movieId);
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
